import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

public class AddAssetUI extends AssetForm{

	private JButton btnAdd;
	
	public AddAssetUI()
	{
		initAdditionalGUI();
		initEventHandlers();
	}
	
	private void initAdditionalGUI()
	{
		btnAdd = new JButton("Add This Asset");
		btnAdd.setBounds(244, 407, 112, 24);
		add(btnAdd);
	}
	
	private void initEventHandlers()
	{
		AddAssetHandler handler = new AddAssetHandler();
		btnAdd.addActionListener(handler);
	}
	
	class AddAssetHandler implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			if(e.getSource() == btnAdd)
			{
				String name = txtName.getText().trim();
				String owner = txtOwner.getText().trim();
				String custodian = txtCustodian.getText().trim();
				Date dateAcquired = datePickerDateAcquired.getDate();
				Date retentionPeriod = datePickerRetentionPeriod.getDate();
				String storageLocation = txtStorageLocation.getText().trim();
		
				String availabilityCode = getCodeFromCDP(availabilityCDP, comboAvailability);
				String classificationCode = getCodeFromCDP(classificationCDP, comboClassification);
				String confidentialityCode = getCodeFromCDP(confidentialityCDP, comboConfidentiality);
				String maintenanceScheduleCode = getCodeFromCDP(maintenanceCDP, comboMaintenance);
				String financialCode = getCodeFromCDP(financialCDP, comboFinancial);
				String integrityCode = getCodeFromCDP(integrityCDP, comboIntegrity);
				String typeCode = getCodeFromCDP(typeCDP, comboType);
				
				//Check if acquired date is before retention period
				boolean isAcquiredDateBeforeRetention = retentionPeriod.compareTo(dateAcquired) > 0;
				boolean isAnyTextFieldEmpty = name.isEmpty() || owner.isEmpty() || custodian.isEmpty() || storageLocation.isEmpty();
				String message = "";
				boolean error = !isAcquiredDateBeforeRetention || isAnyTextFieldEmpty ;
				
				if(error)
				{
					//Highlight empty text fields
					if(name.isEmpty())
						txtName.setBackground(Color.PINK);
					else
						txtName.setBackground(Color.WHITE);
					
					if(owner.isEmpty())
						txtOwner.setBackground(Color.PINK);
					else
						txtOwner.setBackground(Color.WHITE);
					
					if(custodian.isEmpty())
						txtCustodian.setBackground(Color.PINK);
					else
						txtCustodian.setBackground(Color.WHITE);
					
					if(storageLocation.isEmpty())
						txtStorageLocation.setBackground(Color.PINK);
					else
						txtStorageLocation.setBackground(Color.WHITE);
					
					if(isAnyTextFieldEmpty)
						message += "Please do not leave any fields blank.\n";
							
					if(!isAcquiredDateBeforeRetention)
					{
						//Display somewhere in UI that acquired date must come before retention period
						message += "Retention Period must be after Acquired Date.\n";
					}
					
					JOptionPane.showMessageDialog(null, message, "Error!", JOptionPane.WARNING_MESSAGE);
				}
				else{
				//Execute 
					txtName.setBackground(Color.WHITE);
					txtOwner.setBackground(Color.WHITE);
					txtCustodian.setBackground(Color.WHITE);
					txtStorageLocation.setBackground(Color.WHITE);
					
					int newID = Asset.insertNewAssetIntoDBAndReturnNewID(name, owner, custodian, typeCode, dateAcquired, retentionPeriod, maintenanceScheduleCode, financialCode, confidentialityCode, integrityCode, availabilityCode, classificationCode, storageLocation);
					LogMethods log = new LogMethods();
					if(newID != -1)
					{
						log.AddLog(Asset.createAssetFromID(newID));
						clearForm();
						message += "Successfully added "+name+".";
					}
					else
						message += "Error adding the new asset.";
					
					JOptionPane.showMessageDialog(null, message);
				}	
			}
		}
		
	
		private String getCodeFromCDP(ArrayList<CodeDescriptionPair> codeDescriptionPairs, JComboBox<String> comboBox)
		{
			return codeDescriptionPairs.get(comboBox.getSelectedIndex()).getCode();
		}
	}
}
